<?php
include("model/categories.php");
class categoriesController{
	
	function indexAction(){
		$smarty = $_SESSION['smarty'];
		
		$list_categories_box = $this->getListCategoryBox();
		$smarty->assign("list_categories_box",$list_categories_box);
		
		$content = $smarty->fetch("categories/categories.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function getListCategoryBox(){
		$smarty 	= $_SESSION['smarty'];
		$category_obj 	= new categories();
		$categoryList	= $category_obj->getCategoriesLevel1List();		
		$smarty->assign("categoryList", $categoryList);
		$html 		= $smarty->fetch("categories/categories_list.html");
		return $html;
	}
	
	function insertAction(){
		$categories_obj = new categories();
		$data 		= array();
		$data['cat_name'] = $_REQUEST['cat_name'];
		$data['cat_cat_id'] = $_REQUEST['cat_cat_id'];
		
		
		$result = $categories_obj->insertACategory($data);
		if ($result == 1)
			$msg = "insert successfully";
		else if ($result == -1)
			$msg = "duplicated!";
		else
			$msg = "insert failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("categories/categories_insertResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
		
	}
	
	function inactiveAction(){
		$cat_id = $_REQUEST['args'];
		$categories_obj = new categories();
		$result = $categories_obj->inactivateACategory($cat_id);
		if ($result == 1)
			$msg = "inactivate successfully";		
		else
			$msg = "inactivate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_categories_box = $this->getListCategoryBox();
		$smarty->assign("list_categories_box",$list_categories_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("categories/categories.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function activeAction(){
		$cat_id = $_REQUEST['args'];
		$categories_obj = new categories();
		$result = $categories_obj->activateACategory($cat_id);
		if ($result == 1)
			$msg = "activate successfully";		
		else
			$msg = "activate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_categories_box = $this->getListCategoryBox();
		$smarty->assign("list_categories_box",$list_categories_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("categories/categories.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function insertFormAction(){
		$smarty = $_SESSION['smarty'];	
			
		$categoriesList = array();
		$categories_obj = new categories();
		$categoriesList = $categories_obj->getParentCategoryList();
		
		$smarty->assign("categoriesList", $categoriesList);
		$insertform_categories_box = $smarty->fetch("categories/categories_insert_form.html");
		$smarty->assign("insertform_categories_box",$insertform_categories_box);
		
		$content = $smarty->fetch("categories/categories.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function editFormAction(){
		$cat_id 			= $_REQUEST['args'];
		$smarty 			= $_SESSION['smarty'];		
		
		$categories_obj 	= new categories();
		$current_data 		= array();
		$current_data 		= $categories_obj->getCategoriesInfo($cat_id);
		
		$smarty->assign("current_data", $current_data);
		
		$categoriesList = $categories_obj->getParentCategoryList();
		$smarty->assign("categoriesList", $categoriesList);
		
		$editform_categories_box = $smarty->fetch("categories/categories_edit_form.html");
		$smarty->assign("editform_categories_box",$editform_categories_box);
		
		$content = $smarty->fetch("categories/categories.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
		
	}
	
	function editAction(){
		$categories_obj = new categories();
		$data 		= array();
		$data['cat_name'] 	= $_REQUEST['cat_name'];
		$data['cat_id'] 		= $_REQUEST['cat_id'];
		
		$result = $categories_obj->editACategory($data);
		
		if ($result == 1)
			$msg = "edit successfully";
		else if ($result == -1)
			$msg = "duplicated!";
		else
			$msg = "update failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("categories/categories_updateResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
		
}
?>